/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.pool  BaseExecutorThread.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-19 上午10:11:37 
 *********************************************************************************/


package com.adc.parallel.pool;

import com.adc.parallel.constants.SysConstants;
import com.adc.parallel.dto.BaseTaskDto;
import com.adc.quicktools.file.FileControl;


/**
 * 
 * Description: base thread template 
 * @author shen  shenqiao2012@163.com
 * @param <E>
 * @param <T>
 */
public abstract class BaseExecutorThread<E extends BaseTaskDto<T>, T> extends Thread {
	private String preReturnInfo;
	private String lastReturnInfo;
	private int execReturn;
	private E taskDto;
	private String startTime;
	private String completeTime;
	
	/**
	 * 
	 * Description: do something before operation
	 * author:  shen
	 * @return String
	 */
	public abstract String beforeOpera();
	
	/**
	 * 
	 * Description: do something after operation 
	 * author:  shen
	 * @return String
	 */
	public abstract String afterOpera();
	
	/**
	 * 
	 * Description:  the execute logic 
	 * author:  shen
	 * @param taskDto
	 * @return int
	 */
	public abstract int process(E taskDto);
	
	public void setTaskDto(E taskDto) {
		this.taskDto=taskDto;
	}
	
	/**
	 * Description:  run 
	 * @see java.lang.Thread#run() 
	 */
	public void run() {
		execReturn=SysConstants.Default_Exec_Return;
		try {
			startTime=FileControl.getCurrentTimeString(SysConstants.EXEC_Time_Format);
			preReturnInfo=beforeOpera();
			execReturn=process(taskDto);
			lastReturnInfo=afterOpera();
		} catch (Exception e) {
			throw new RuntimeException("Task executor exist Error, a few Exceptions are not processed");
		}finally{
			completeTime=FileControl.getCurrentTimeString(SysConstants.EXEC_Time_Format);
		}
	}

	/**
	 * @return the preReturnInfo
	 */
	public String getPreReturnInfo() {
		return preReturnInfo;
	}

	/**
	 * @return the lastReturnInfo
	 */
	public String getLastReturnInfo() {
		return lastReturnInfo;
	}

	/**
	 * @return the execReturn
	 */
	public int getExecReturn() {
		return execReturn;
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @return the completeTime
	 */
	public String getCompleteTime() {
		return completeTime;
	}
	
	
	
}
